import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutterapp/Base/CusLoading.dart';
import 'file:///C:/Users/41784/AndroidStudioProjects/remoteCtrl/flutter_app/lib/Page/PageHome.dart';
import 'package:flutterapp/SaasManager.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:wave/config.dart';
import 'package:wave/wave.dart';

class PageReg extends StatefulWidget {
  static const ROUTER_NAME = "PageReg";

  @override
  _PageRegState createState() => _PageRegState();
}

class _PageRegState extends State<PageReg> {
  TextEditingController _nameCtrl = new TextEditingController();
  TextEditingController _pswCtrl = new TextEditingController();
  FocusNode focusNodeUserName = new FocusNode();
  FocusNode focusNodePassword = new FocusNode();

  @override
  void initState() {
    super.initState();
    _nameCtrl.text = "";
    _pswCtrl.text = "";
    WidgetsBinding.instance.addPostFrameCallback((_) {
      SaasManager.isLogin().then((value) {
        if (value) {
          Navigator.of(context)
              .pushNamedAndRemoveUntil(PageHome.ROUTER_NAME, (route) => false);
        }
      });
    });
  }

  @override
  void dispose() {
    _nameCtrl.dispose();
    _pswCtrl.dispose();
    print("pr dispose");
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text("登录"),
      ),
      body: Stack(
        children: <Widget>[
          Offstage(
            offstage: false,
            child: WaveWidget(
              config: CustomConfig(
                gradients: [
                  [Colors.red, Color(0xEEF44336)],
                  [Colors.red[800], Color(0x77E57373)],
                  [Colors.orange, Color(0x66FF9800)],
                  [Colors.yellow, Color(0x55FFEB3B)]
                ],
                durations: [35000, 19440, 10800, 6000],
                heightPercentages: [0.20, 0.23, 0.25, 0.30],
                blur: MaskFilter.blur(BlurStyle.solid, 10),
                gradientBegin: Alignment.bottomLeft,
                gradientEnd: Alignment.topRight,
              ),
//      colors: [
//        Colors.white70,
//        Colors.white54,
//        Colors.white30,
//        Colors.white24,
//      ],
//      durations: [
//        32000,
//        21000,
//        18000,
//        5000,
//      ],
              waveAmplitude: 0,
//      heightPercentages: [0.25, 0.26, 0.28, 0.31],
              backgroundColor: Colors.blue,
              size: Size(
                double.infinity,
                double.infinity,
              ),
            ),
          ),
          Container(
            padding: EdgeInsets.all(10),
            child: SingleChildScrollView(
              child: Column(
                children: <Widget>[
                  SizedBox(
                    height: 180,
                  ),
                  TextField(
                    controller: _nameCtrl,
                    focusNode: focusNodeUserName,
                    decoration: InputDecoration(
                        border: OutlineInputBorder(),
                        hintText: '请输入userName',
                        labelText: 'userName',
                        prefixIcon: Icon(Icons.phone_android),
                        helperText: "为保证唯一性，建议用手机号作为用户名"),
                    onEditingComplete: () {
                      FocusScope.of(context).requestFocus(focusNodePassword);
                    },
                  ),
                  SizedBox(
                    height: 18,
                  ),
                  TextField(
                    controller: _pswCtrl,
                    focusNode: focusNodePassword,
                    decoration: InputDecoration(
                      border: OutlineInputBorder(),
                      hintText: '请输入password',
                      labelText: 'password',
                      prefixIcon: Icon(Icons.security),
                    ),
                    obscureText: true,
                  ),
                  SizedBox(
                    height: 18,
                  ),
                  SizedBox(
                    child: FlatButton(
                      onPressed: () {
                        if (_pswCtrl.text.isEmpty || _nameCtrl.text.isEmpty) {
                          Fluttertoast.showToast(msg: "账号密码不能为空");
                          return;
                        }
                        SaasManager.autoLogin(_nameCtrl.text, _pswCtrl.text);
                      },
                      child: Text(
                        "注册&登录",
                        style: TextStyle(fontSize: 18),
                      ),
                      padding: EdgeInsets.symmetric(horizontal: 8),
                      //文字颜色
                      textColor: Colors.white,
                      //按钮颜色
                      color: Theme.of(context).primaryColor,
                      //画圆角
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(54),
                      ),
                    ),
                    height: 54,
                    width: 240,
                  )
                ],
              ),
            ),
            decoration: BoxDecoration(
              color: Colors.transparent,
            ),
          ),
        ],
      ),
    );
  }
}
